import 'package:flutter/material.dart';
import './tools/keepAliveWrapper.dart';

void main(List<String> args) {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // debugger图标
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  // 生命周期函数，当组件初始化时被触发
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 8, vsync: this);
    _tabController.addListener(() {
      if (_tabController.animation!.value == _tabController.index) {
        print(_tabController.index); // 获取点击或滑动时，索引值
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(onPressed: () {}, icon: Icon(Icons.menu)),
        backgroundColor: Colors.red,
        title: Container(
          alignment: Alignment.center,
          child: Text('Flutter app'),
        ),
        actions: [
          IconButton(onPressed: () {}, icon: Icon(Icons.search)),
          IconButton(onPressed: () {}, icon: Icon(Icons.more_horiz)),
        ],
        // 右侧按钮
        bottom: TabBar(
          isScrollable: true, // 是否可以滑动
          indicatorColor: Colors.white,
          indicatorPadding: EdgeInsets.all(5),
          labelColor: Colors.yellow, // 选中的颜色
          unselectedLabelColor: Colors.white, //未选中的颜色
          controller: _tabController, //注意：配置controller要去掉 TabBar的const
          tabs: const [
            Tab(child: Text("关注")),
            Tab(child: Text("热门")),
            Tab(child: Text("视频")),
            Tab(child: Text("娱乐")),
            Tab(child: Text("篮球")),
            Tab(child: Text("深圳")),
            Tab(child: Text("疫情")),
            Tab(child: Text("其他")),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          KeepAliveWrapper(
            keepAlive: true,
            child: ListView(
              children: [
                ListTile(title: Text("我是关注")),
                ListTile(title: Text("我是关注")),
                ListTile(title: Text("我是关1注")),
                ListTile(title: Text("我是关2注")),
                ListTile(title: Text("我是关注")),
                ListTile(title: Text("我是关23注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关注")),
                ListTile(title: Text("我是关124注")),
                ListTile(title: Text("我是关14注")),
                ListTile(title: Text("我是关412注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关4注")),
                ListTile(title: Text("我是关422注")),
              ],
            ),
          ),
          const Text("我是热门"),
          const Text("视频"),
          const Text("娱乐"),
          const Text("篮球"),
          const Text("深圳"),
          const Text("疫情"),
          const Text("其他"),
        ],
      ),
    );
  }
}
